package com.springboot.mongodb.controller;

import com.springboot.mongodb.impl.UserServiceImpl;
import com.springboot.mongodb.model.AddUserDTO;
import com.springboot.mongodb.model.FindUserDTO;
import com.springboot.mongodb.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/User")
@Slf4j
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    /**
     * 添加用户
     *
     * @param addUserDTO
     * @return
     */
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public User addUser(@RequestBody AddUserDTO addUserDTO) {
        return userService.addUser(addUserDTO);
    }

    /**
     * 查询所有用户
     *
     * @param findUserDTO
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    public Page<User> findAll(@RequestBody FindUserDTO findUserDTO) {
        return userService.findAll(findUserDTO);
    }

    /**
     * 根据条件查询
     *
     * @param findUserDTO
     * @return
     */
    @RequestMapping(value = "/getByName", method = RequestMethod.POST)
    public User findByName(@RequestBody FindUserDTO findUserDTO) {
        return userService.findByName(findUserDTO);
    }

    /**
     * 根据ID删除
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/delById", method = RequestMethod.POST)
    public boolean del(@RequestBody User user) {
        userService.del(user);
        return true;
    }

    /**
     * 更新
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public User upd(@RequestBody User user) {
        return userService.upd(user);
    }
}
